﻿/*****************************************************************************
// *
// * Copyright (c) Microsoft Corporation. All rights reserved.
// * 
// * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
// * 
// **************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.VisualStudio.CommandBars;

namespace Tcvt.Packages
{
    /// <summary>
    /// Represents an add-in command which hooks the SCE context menu.
    /// </summary>
    internal abstract class SCEAddInCommand : AddInCommand
    {
        /// <summary>
        /// Called to register this command.
        /// </summary>
        protected override void UISetup(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            base.UISetup(application, connectMode, addInInst, ref custom);

            AddCommandToSCEContextMenu(SCEContextMenuPosition);
        }

        /// <summary>
        /// Registers this command on the SCE context menu at the specified position.
        /// </summary>
        /// <param name="sceRelativePosition">Position from the top of the SCE context menu</param>
        private void AddCommandToSCEContextMenu(int sceRelativePosition)
        {
            object[] contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)m_applicationObject.Commands;
            CommandBars cmdBars = (CommandBars)m_applicationObject.CommandBars;
            CommandBar sceCommandBar = cmdBars["Source Control Explorer"];

            try
            {
                Command command = m_dteCommand;

                if ((command != null) && (sceCommandBar != null))
                {
                    command.AddControl(sceCommandBar, s_sourceControlBaseMenuPosition + sceRelativePosition);
                }
            }
            catch (ArgumentException) { }
        }

        /// <summary>
        /// Returns the position on the SCE context menu for this addin command.
        /// </summary>
        protected abstract int SCEContextMenuPosition
        {
            get;
        }

        /// <summary>
        /// Returns a reference to the extensibility interface for the Source Control Explorer.
        /// </summary>
        protected VersionControlExplorerExt SourceControlExplorer
        {
            get
            {
                if (null == m_sourceControlExplorer)
                {
                    // Get and keep a reference to the Source Control Explorer extensibility interface.
                    VersionControlExt vce = ((DTE2)m_applicationObject).GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt;
                    m_sourceControlExplorer = vce.Explorer;
                }

                return m_sourceControlExplorer;
            }
        }

        #region IsSCEActive

        /// <summary>
        /// Returns true if the Source Control Explorer is the active window, false otherwise.
        /// </summary>
        protected bool IsSCEActive
        {
            get
            {
                if (m_applicationObject == null)
                {
                    return false;
                }

                Window2 activeWindow = (Window2)m_applicationObject.ActiveWindow;
                if (activeWindow == null)
                {
                    return false;
                }

                Guid guidCurWin = new Guid(activeWindow.ObjectKind);
                if (guidCurWin == SourceControlExplorerGuid)
                {
                    return true;
                }

                return false;
            }
        }

        #endregion

        // Guid for the SCE toolwindow
        internal static readonly Guid SourceControlExplorerGuid = new Guid("99B8FA2F-AB90-4F57-9C32-949F146F1914");
        
        // Base index for SCE menu position
        private static readonly int s_sourceControlBaseMenuPosition = 0;

        // Source control explorer extensibility interface
        private VersionControlExplorerExt m_sourceControlExplorer = null;
    }
}
